package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {


    @Autowired
    private CheckGroupDao checkGroupDao;
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        setCheckGroupAndCheckItem(checkGroup,checkitemIds);
    }

    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page =checkGroupDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemOIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemOIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
      //修改检查组的基本信息
        checkGroupDao.editGroupById(checkGroup);
        //清空检查项的数据
        checkGroupDao.deleteCheckItemsId(checkGroup.getId());
        //为检查项数据重新赋值
        setCheckGroupAndCheckItem(checkGroup,checkitemIds);
    }

    @Override
    public void delete(Integer id) {

        //根据检查组id查询是否关联了套餐
      long mealCount = checkGroupDao.relationMeal(id);
//        根据检查组id查询是否关联了检查项
        long itemCount = checkGroupDao.findCheckItemIdByCenterTable(id);
       // checkGroupDao.delete(id);

        if(mealCount>0 || itemCount >0){
            throw new RuntimeException("数据有关联,无法删除");
        }else {
             checkGroupDao.delete(id);
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    //抽取方法  为检查项添加信息

    public void setCheckGroupAndCheckItem(CheckGroup checkGroup, Integer[] checkitemIds){
        Integer checkGroupId = checkGroup.getId();
        if(checkitemIds!=null && checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkGroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItems(map);

            }
        }
    }

}
